#include "world.h"

namespace tri{



TWorld::TWorld() : tri::TEntity(){
}

TWorld::~TWorld(){
	clear();
}

void TWorld::display(){
}

void TWorld::clear(){
	clearMap();
	clearTileCache();
}

void TWorld::clearMap(){
	for (unsigned int i=0 ; i < mTiles.size() ; i++){
		delete mTiles[i];
	}
	mTiles.clear();
}

void TWorld::clearTileCache(){
	mTileCache.clear();
}


void TWorld::loadTiles(std::string xmlfile){
	tri::TXMLDoc _doc;
	_doc.load(xmlfile.c_str());

	if(_doc.hasElement("tile_doc")){
		tri::TXMLNode* doc = _doc.getElement("tile_doc");
		int tileCount = doc->countElement("tile");
		
		for (int i=0; i < tileCount; i++){
			tri::TXMLNode* tile_node = doc->getElement("tile",i);
			mTileCache.addTemplate(tile_node);
		}
	}
}
			
//			//tile type
//			tile->mTileType = getTileTypeFromString(tile_node->getAttribute<std::string>("type"));
//			
//			//tile id
//			unsigned int id = (unsigned int)tile_node->getAttribute<int>("id");
//			tile->mID - id;
//			
//			if(tile_node->hasAttribute("image")){
//				tile->mImage = tile_node->getAttribute<std::string>("image");
//			}
//			
//			if(tile_node->hasAttribute("tileImage")){
//				tile->mTileImage = tile_node->getAttribute<std::string>("tileImage");
//			}
//			
//			if(tile_node->hasAttribute("model")){
//				tile->mModel = tile_node->getAttribute<std::string>("model");
//			}
//			
//			if(tile_node->hasAttribute("icon")){
//				tile->mIcon = tile_node->getAttribute<std::string>("icon");
//			}
//			
//			if(tile_node->hasAttribute("dimX")){
//				tile->dimensionX = tile_node->getAttribute<int>("dimX");
//			}
//			
//			if(tile_node->hasAttribute("dimY")){
//				tile->dimensionY = tile_node->getAttribute<int>("dimY");
//			}
//			
//			if(tile_node->hasElement("rotation")){
//				tile->rot = loadVector(tile_node->getElement("rotation"));
//			}
//			
//			if(tile_node->hasElement("size")){
//				tile->size = loadVector(tile_node->getElement("size"));
//			}
//			
//			if(tile_node->hasElement("position")){
//				tile->pos = loadVector(tile_node->getElement("position"));
//			}
//			
//			if(tile_node->hasElement("scale")){
//				tile->scale = loadVector(tile_node->getElement("scale"));
//			}
//			
//			if(tile_node->hasElement("texture")){
//			
//				int textureCount = tile_node->countElement("texture");
//				tri::TXMLNode* tex_node = NULL;
//				std::string meshName = "";
//				std::string image = "";
//				
//				for (int i = 0; i < textureCount ; i++){
//					meshName = "";
//					image = "";
//					tex_node = tile_node->getElement("texture",i);
//					
//					
//					if(tex_node->hasAttribute("mesh")){
//						meshName = tex_node->getAttribute<std::string>("mesh");
//					}
//					
//					image = tex_node->getAttribute<std::string>("image");
//					tile->mTextures[meshName] = image;
//				}
//			}
//			
//			
//			addTileType(id, tile);
//			
//		}
//	}
//	
//}

void TWorld::addTile(TVector pos, TilePlane* tile, tri::TEntity* object){
	tile->setPosition(pos);
	addEntity(tile);
	mTiles.push_back(tile);
}

void TWorld::loadMap(std::string file){
	std::string line;
	std::ifstream myfile (file.c_str());
	
	float x = 0;
	float z = 0;
	if (myfile.is_open())
	{
		while ( myfile.good() )
		{
			std::getline (myfile,line);
			std::string tileID = "";
			for (unsigned int i =0 ;i<line.size();i++){
			
				char c = line.c_str()[i];
				if (c != ' '){
					tileID.append(&c,1);
				}else if (tileID != ""){
					unsigned int id = (unsigned int)tri::utils::string2Int(tileID.c_str());
					TilePlane* tile = mTileCache.getTile(id);
					if (tile != NULL){
						addTile(TVector(x,0,z), tile);
					}
					tileID = "";
					
					x+=1.0;
				
				}
				
			}
			x=0;
			z+=1;
		}
		myfile.close();
	}

	else{
		tri::LOG_SEVERE("Unable to load "+file);
		tri::utils::quit();
	}
}

}
